package com.cloud.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.mapper.LinkmanMapper;
import com.cloud.pojo.Linkman;
import com.cloud.pojo.LinkmanExample;
import com.cloud.pojo.LinkmanExample.Criteria;
import com.cloud.service.LinkmanServiec;

@Service
public class LinkmanServiceImpl implements LinkmanServiec {
	
	
	@Autowired
	private LinkmanMapper linkmanMapper;
	
	@Override
	public Integer addLinkman(Linkman linkman) {
		// TODO Auto-generated method stub
		return linkmanMapper.insertSelective(linkman);
	}

	@Override
	public Integer deleteLinkman(Integer adminId,String id) {
		// TODO Auto-generated method stub
//		LinkmanExample example = new LinkmanExample();
//		Criteria criteria = example.createCriteria();
//		criteria.andAdminIdEqualTo(adminId);
//		criteria.andPhoneEqualTo(phone);
//		linkmanMapper.deleteByPrimaryKey(phone);
		return linkmanMapper.deleteByPrimaryKey(id);
	}

	@Override
	public Integer updateLinkman(Integer adminId,Linkman linkman) {
		Integer r = 0;
		LinkmanExample example = new LinkmanExample();
		Criteria criteria = example.createCriteria();
		criteria.andAdminIdEqualTo(adminId);
		criteria.andIdEqualTo(linkman.getId());
		List<Linkman> list = linkmanMapper.selectByExample(example);
		if(list != null && list.size()>0){
//			linkman.setId(list.get(0).getId());
			linkman.setUpdateTime(new Date());
			r = linkmanMapper.updateByPrimaryKeySelective(linkman);
		}else {
			linkman.setAdminId(adminId);
			linkman.setCreateTime(new Date());
			r = linkmanMapper.insertSelective(linkman);
		}
		return r;
	}

	@Override
	public List<Linkman> selectLinkmanList(Integer adminId) {
		// TODO Auto-generated method stub
		LinkmanExample example = new LinkmanExample();
		Criteria criteria = example.createCriteria();
		criteria.andAdminIdEqualTo(adminId);
		List<Linkman> list = linkmanMapper.selectByExample(example);
		return list;
	}

	@Override
	public Integer addLinkmanList(Linkman linkman) {
		// TODO Auto-generated method stub
		Integer r = 0;
		LinkmanExample example = new LinkmanExample();
		Criteria criteria = example.createCriteria();
		criteria.andAdminIdEqualTo(linkman.getAdminId());
		criteria.andPhoneEqualTo(linkman.getPhone());
		List<Linkman> list = linkmanMapper.selectByExample(example);
		if(list != null && list.size()>0){
			linkman.setId(list.get(0).getId());
			linkman.setUpdateTime(new Date());
			r = linkmanMapper.updateByPrimaryKeySelective(linkman);
		}else {
			linkman.setCreateTime(new Date());
//			linkman.setId(null);
			r = linkmanMapper.insertSelective(linkman);
		}
		return r;
	}

}
